package com.petrooil.core.dao;

import com.petrooil.core.model.PetroOilDoc;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

@Repository("petroOilDocDao")
public class PetroOilDocDaoImpl implements PertoOilDocDao {

    @Autowired
    private SessionFactory sessionFactory;

    @Override
    public PetroOilDoc create(PetroOilDoc petroOilDoc) {
        sessionFactory.getCurrentSession().persist(petroOilDoc);
        return petroOilDoc;
    }

    @Override
    public PetroOilDoc get(Serializable id) {
        return (PetroOilDoc) sessionFactory.getCurrentSession().get(PetroOilDoc.class, id);
    }

    @Override
    public PetroOilDoc update(PetroOilDoc petroOilDoc) {
        sessionFactory.getCurrentSession().update(petroOilDoc);
        return petroOilDoc;
    }

    @Override
    public void delete(PetroOilDoc petroOilDoc) {
        sessionFactory.getCurrentSession().delete(petroOilDoc);
    }

    @Override
    public PetroOilDoc getOrbeonDocById(Serializable id) {
        return null;
    }

    @Override
    public List<PetroOilDoc> getPetroDocList(Serializable department) {
        List<PetroOilDoc> list;
        list = sessionFactory.getCurrentSession().createQuery("from PetroOilDoc where inDepartment=?")
                .setParameter(0, department).list();
        return list;
    }

    @Override
    public long countTasks(String department) {
        return (long) sessionFactory.getCurrentSession().createQuery("select count(documentId) from PetroOilDoc where department=?")
                .setParameter(0, department).uniqueResult();
    }
}
